define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order_integral/index' + location.search,
                    add_url: 'order_integral/add',
                    edit_url: 'order_integral/edit',
                    // del_url: 'order/del',
                    multi_url: 'order_integral/multi',
                    import_url: 'order_integral/import',
                    table: 'order_integral',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'userhome_id', title: __('userhome_id')},
                        {field: 'userhome.nickname', title: __('用户名称')},
                        {field: 'ordernum', title: __('兑换编号'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'),operate:false, searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5'),"9":__('Status 9')}, formatter: Table.api.formatter.status},
/*                        {field: 'paytype', title: __('Paytype'), searchList: {"1":__('Paytype 1'),"2":__('Paytype 2'),"3":__('Paytype 3'),"4":__('Paytype 4')}, formatter: Table.api.formatter.normal},*/
                        // {field: 'is_yc', title: __('Is_yc'), searchList: {"1":__('Is_yc 1'),"0":__('Is_yc 0')}, formatter: Table.api.formatter.normal},
                        {field: 'goods_name', title: __('商品名称'), operate: 'LIKE'},
                        {field: 'goods_spec_name', title: __('商品规格'), operate: 'LIKE'},
                        {field: 'goods_num', title: __('商品数量')},
                        {field: 'goods_price', title: __('商品单价')},
                        {field: 'allprice', title: __('商品总价')},
                        {field: 'use_integral', title: __('实付积分')},
                        {field: 'supplier.suppname', title: __('Supplier.suppname'), operate: 'LIKE'},
                        {field: 'goods_cost_price', title: __('供应商单价')},
                        {field: 'supplier_cost', title: __('供应商总价')},
                        {field: 'shuilv', title: __('税率')},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'fahuotime', title: __('Fahuotime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'shouhuotime', title: __('Shouhuotime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'wuliu_num', title: __('Wuliu_num'), operate: 'LIKE'},
                        // {field: 'wuliu_code', title: __('Wuliu_code'), operate: 'LIKE'},
                        {field: 'wuliu_name', title: __('Wuliu_name'), operate: 'LIKE'},
                        {field: 'tx_fahuo', title: __('Tx_fahuo'), operate:false},
                        // {field: 'invoice_id', title: __('Invoice_id')},
                        {field: 'id', title: __('查看详情'), formatter: Controller.api.formatter.id,operate:false},
                        {field: 'is_check', title: __('Is_check'), operate:false,searchList: {"0":__('Is_check 0'),"1":__('Is_check 1'),"2":__('Is_check 2')}, formatter: Table.api.formatter.normal},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [

                                {
                                    name: 'detail',
                                    title: __('查看商品'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'order/detail'
                                },
                                {
                                    name: 'detail',
                                    title: __('开发票'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-newspaper-o',
                                    url: 'order/fapiao',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.is_kaipiao == '1'){ 
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'detail',
                                    title: __('发货'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-truck',
                                    url: 'order/delivery',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status == '2'){ 
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'detail',
                                    title: __('物流详情'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-edge',
                                    url: 'order/logistics',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status == '3' || row.status == '4' ||row.status == '5'){ 
                                            return true;
                                        }
                                    }
                                },
                            ]

                        }
                    ]
                ],
                search:false,
                // showColumns: false,
                showExport: false,//导出excel
                showToggle: false,
            });

            $("#exp").bind("click",function(){
                var options = table.bootstrapTable('getOptions');
                var search = options.queryParams({});
                var str=location.search+"&filter="+search.filter+"&op="+search.op;
                window.location.href = 'order_integral/export'+str  ;
            });

            // 绑定TAB事件
            $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var field = $(this).closest("ul").data("field");
                var value = $(this).data("value");
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                var queryParams = options.queryParams;
                options.queryParams = function (params) {
                    params = queryParams(params);
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    if (value !== '' && value != 'all') {
                        filter[field] = value;
                    } else {
                        delete filter[field];
                    }
                    params.filter = JSON.stringify(filter);
                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;
            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        
        delivery: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                id: function (value, row, index) {
                    //这里手动构造URL
                    var ids = row['id'];
                    // var store_id = row['store_id'];
                    
                    url = "orderdetail?order_id=" + ids;
                    //方式一,直接返回class带有addtabsit的链接,这可以方便自定义显示内容
                    return '<a href="' + url + '" class="label label-success addtabsit" title="' + __("查看订单详情", value) + '">' + '查看订单详情' + '</a>';
                    
                   
                }
           }
        }
    };
    return Controller;
});